#include <hpdf.h>
#include <iostream>
#include <string>

const int CHARS_PER_PAGE = 300;
const int CHARS_PER_LINE = 15;

int main() {
    HPDF_Doc pdf = HPDF_New(NULL, NULL);
    if (!pdf) {
        std::cerr << "Failed to create PDF document!" << std::endl;
        return 1;
    }

    HPDF_Font font = HPDF_GetFont(pdf, "Helvetica", NULL);

    //内容
    std::string songti_text = "hello world hello world hello world hello world hello world hello world hello world hello world hello world";

    size_t char_index = 0;
    while (char_index < songti_text.length()) {
        HPDF_Page page = HPDF_AddPage(pdf);
        HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
        HPDF_Page_SetFontAndSize(page, font, 12);

        for (size_t line = 0; line < CHARS_PER_PAGE / CHARS_PER_LINE; ++line) {
            std::string line_text;
            for (size_t i = 0; i < CHARS_PER_LINE; ++i) {
                if (char_index < songti_text.length()) {
                    line_text += songti_text[char_index++];
                } else {
                    break;
                }
            }
            float y = HPDF_Page_GetHeight(page) - (line + 1) * 20 - 20;
            HPDF_Page_BeginText(page);
            HPDF_Page_MoveTextPos(page, 20, y);
            HPDF_Page_ShowText(page, line_text.c_str());
            HPDF_Page_EndText(page);
        }
    }

    // 保存 PDF 文件到当前目录
    if (HPDF_SaveToFile(pdf, "output.pdf") != HPDF_OK) {
        std::cerr << "无法保存 PDF 文件！" << std::endl;
    } else {
        std::cout << "PDF 文件保存成功：output.pdf" << std::endl;
    }

    HPDF_Free(pdf);
    return 0;
}

